package com.guyu.plugin.satoken.config;

import cn.dev33.satoken.dao.SaTokenDao;
import cn.dev33.satoken.stp.StpInterface;
import cn.dev33.satoken.strategy.SaStrategy;
import com.guyu.plugin.satoken.core.PlusSaTokenDao;
import com.guyu.plugin.satoken.core.StpInterfaceImpl;
import com.guyu.plugin.satoken.listener.GySaTokenListener;
import com.guyu.plugin.satoken.service.SaTokenHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.AutoConfiguration;
import org.springframework.context.annotation.Bean;
import org.springframework.core.annotation.AnnotatedElementUtils;


@AutoConfiguration
public class SaTokenAutoConfiguration {

    /**
     * 自定义dao层存储
     */
    @Bean
    public SaTokenDao saTokenDao() {
        return new PlusSaTokenDao();
    }

    @Bean
    public SaTokenHelper saTokenHelper() {
        return new SaTokenHelper();
    }

    @Bean
    public GySaTokenListener gySaTokenListener() {
        return new GySaTokenListener();
    }

    /**
     * 权限接口实现(使用bean注入方便用户替换)
     */
    @Bean
    public StpInterface stpInterface(SaTokenHelper saTokenHelper) {
        return new StpInterfaceImpl(saTokenHelper);
    }

    /**
     * 重写 Sa-Token 框架内部算法策略
     */
    @Autowired
    public void rewriteSaStrategy() {
        // 重写Sa-Token的注解处理器，增加注解合并功能
        SaStrategy.instance.getAnnotation = AnnotatedElementUtils::getMergedAnnotation;
    }
}
